<?php

class Catproduct extends AppModel {

    var $alias = 'Catproduct';
    var $name = 'Catproduct';
    var $useTable = 'product_categories';
    var $hasMany = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'cat_id',
            'dependent' => true,
        )
    );
    var $belongsTo = array(
        'UserModify' => array(
            'className' => 'User',
            'foreignKey' => 'user_modified',
            'fields' => array('User.id', 'User.username')
        ),
        'UserCreated' => array(
            'className' => 'User',
            'foreignKey' => 'user_created',
            'fields' => array('User.id', 'User.username')
        )
    );

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function getChild($parent_id = 0) {
        $dbrows = $this->find('all', array(
                    'conditions' => array('parent_id' => $parent_id),
                    'fields' => 'id, title, parent_id,level,alias',
                    'recursive' => -1,
                        )
        );
        return ($dbrows);
    }

    function getChildAll($parent_id = 0, &$dbrow) {
        $dbrows = $this->find('all', array(
                    'conditions' => array('parent_id' => $parent_id),
                    'fields' => 'id, title, parent_id',
                    'recursive' => -1,
                        )
        );
        if (!is_array($dbrows) || sizeof($dbrows) == 0) {
            return $dbrow;
        } else {
            foreach ($dbrows as $row) {
                $dbrow[] = $row;
                $this->getChildAll($row[$this->alias]['id'], $dbrow);
            }
        }
    }

    function getChildAllById($parent_id = 0, &$dbrow) {
        $dbrows = $this->find('all', array(
                    'conditions' => array('parent_id' => $parent_id),
                    'fields' => 'id',
                    'recursive' => -1,
                        )
        );

        if (!is_array($dbrows) || sizeof($dbrows) == 0) {
            return $dbrow;
        } else {
            foreach ($dbrows as $row) {
                $dbrow[] = $row[$this->alias]['id'];
                $this->getChildAllById($row[$this->alias]['id'], $dbrow);
            }
        }
    }

    function getParent($parent_id = 0) {
        $dbrows = $this->find('first', array(
                    'conditions' => array('id' => $parent_id),
                    'fields' => 'id, title, parent_id',
                    'recursive' => -1,
                        )
        );
        return $dbrows;
    }

    function getAll() {
        $dbrows = $this->find('all', array(
                    'conditions' => array('published' => '1'),
                    'recursive' => -1,
                        )
        );
        return $dbrows;
    }
    function cacheCatList($parentid, $cache_name){
        $catids =   array($parentid);
        $this->getChildAllById($parentid, $catids);
        Cache::write($cache_name, $catids, 'product');
    }
    function writeCatProductRootMenu() {

        $html = '<ul class="submenu">' . "\n";
        $this->writeCat(0, 0, $html);
        $html .= '</ul>';
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'catproductroot_menu.ctp', 'w');
        fwrite($resourse, $html);
    }

    function writeCat($parent_id = 0, $level = 0, &$html) {

        $open_ul = false;
        $dbrow = array();
        $dbrow = $this->find('all', array(
                    'conditions' => array('parent_id' => $parent_id, 'published' => '1'),
                    'fields' => 'id, title, parent_id, level, alias',
                    'recursive' => -1,
             )
        );
        if (sizeof($dbrow) <= 0) {
            return;
        }
        foreach ($dbrow as $row) {
            if ($level == 0) {
                $class_css = ' class="danhsach1"';
                $ul_sub_class = ' class="submenu1"';
            }else{
                $class_css  =   '';
            }
            $has_child = $this->find('all', array(
                    'conditions' => array('parent_id' => $row['Catproduct']['id'], 'published' => '1'),
                    'fields' => 'id, title, parent_id, level, alias',
                    'recursive' => -1,
                )
            );

            if (sizeof($has_child) > 0) {
                $html .= "<li$class_css><a title=\"{$row['Catproduct']['title']}\" href=\"/san-pham/danh-muc/{$row['Catproduct']['id']}-{$row['Catproduct']['alias']}.html\">{$row['Catproduct']['title']}</a><ul$ul_sub_class>";
                $open_ul = true;
            } else {
                $html .= "<li$class_css><a title=\"{$row['Catproduct']['title']}\" href=\"/san-pham/danh-muc/{$row['Catproduct']['id']}-{$row['Catproduct']['alias']}.html\">" . $row['Catproduct']['title'] . '</a>';
            }
            $this->writeCat($row['Catproduct']['id'], ++$level, $html);
            if ($open_ul) {
                $html .= "</ul></li>\n";
                $open_ul = false;
            }
            else
                $html .= "</li>\n";
            $level--;
        }
    }

}

?>
